package com.noodles.mall.controller;

import com.noodles.mall.entity.ItemType;
import com.noodles.mall.service.ItemService;
import com.noodles.mall.service.ItemTypeService;
import com.noodles.mall.web.JsonResult;
import com.noodles.mall.web.State;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/** 商品类别 **/
@RestController
@RequestMapping("/item-type")
public class ItemTypeController {
    @Autowired
    private ItemTypeService itemTypeService;

    @Autowired
    ItemService itemService;

    /** 增加 **/
    @RequestMapping("/insert")
    public JsonResult insert(@RequestBody ItemType itemType) {
        System.out.println("ItemTypeController: insert");
        System.out.println("itemType: "+itemType);
        if(itemTypeService.STRING_ITEM_TYPE_MAP.containsKey(itemType.getName()))
            JsonResult.error(State.ERR_EXIST);
        return itemTypeService.insert(itemType)
                ? JsonResult.success() : JsonResult.error(State.ERR_SQL);
    }

    /** 修改 **/
    @RequestMapping("/update")
    public JsonResult update(@RequestBody ItemType itemType) {
        System.out.println("ItemTypeController: updateItemType");
        System.out.println("itemType: "+itemType);
        return itemTypeService.update(itemType)
                ? JsonResult.success() : JsonResult.error(State.ERR_SQL);
    }

    /** 查询列表 **/
    @RequestMapping("/selectList")
    public JsonResult selectList() {
        System.out.println("ItemTypeController: selectList");
        List<ItemType> itemTypeList = itemTypeService.selectList();
        return JsonResult.success(itemTypeList);
    }

    /** 商品销量的类别分布 **/
    @RequestMapping("/findTypeRatio")
    public JsonResult findTypeRatio() {
        System.out.println("ItemTypeController: findTypeRatio");
        List<Map<String,Object>> list=itemService.selectSaleCountWithType();
        return JsonResult.success(list);
    }





}
